package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.RiskClassfyCfgDao;
import com.fdb.efp.nls.service.dao.RiskClassfyListDao;
import com.fdb.efp.nls.service.domain.RiskClassfyCfg;
import com.fdb.efp.nls.service.domain.RiskClassfyList;
import com.fdb.efp.nls.service.facade.RiskClassfyCfgService;
import com.fdb.efp.nls.service.vo.RiskClassfyCfgVO;
import com.fdb.efp.nls.service.vo.RiskClassfyListVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>ServiceImpl: 风险分类配置表				<p>
 * @author fuzm
 * @date 2018-05-08
 */
@Service("riskClassfyCfgService")
public class RiskClassfyCfgServiceImpl implements RiskClassfyCfgService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(RiskClassfyCfgServiceImpl.class);

	@Autowired
	private RiskClassfyCfgDao riskClassfyCfgDao;
	@Autowired
	private RiskClassfyListDao riskClassfyListDao;
	/**
	 * -4:若已存在已生效的风险分类配置，则不允许新增风险分类配置!
	 * @return
	 */
	private int valiRiskClassfyCfg() {
		int num = riskClassfyCfgDao.queryEnableRiskCfgCount();
		if ( num > 0 ) num = -4;
		return num;
	}
	/**
	 * 新增操作
	 */
	public int insertRiskClassfyCfg(RiskClassfyCfgVO inRiskClassfyCfgVo) {
		logger.debug("当前新增数据为:"+ inRiskClassfyCfgVo.toString());
		int num = 0;
		try {
			num = valiRiskClassfyCfg();
			if ( num == 0 ) {
				RiskClassfyCfg riskClassfyCfg = new RiskClassfyCfg();
				beanCopy(inRiskClassfyCfgVo, riskClassfyCfg);
				String riskCfgNo = UUID.randomUUID().toString().replace("-", "");
				riskClassfyCfg.setRiskCfgNo(riskCfgNo);
				num = riskClassfyCfgDao.insertRiskClassfyCfg(riskClassfyCfg);
			}
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(RiskClassfyCfgVO inRiskClassfyCfgVo) {
		logger.debug("当前删除数据条件为:"+ inRiskClassfyCfgVo);
		int num = 0;
		try {
			RiskClassfyCfg riskClassfyCfg = new RiskClassfyCfg();
			beanCopy(inRiskClassfyCfgVo, riskClassfyCfg);
			RiskClassfyCfg temp = riskClassfyCfgDao.queryByPk(riskClassfyCfg);
			if ( !"0".equals(temp.getStatus() ) ) {
				num = -2;
			}else {
				num = riskClassfyCfgDao.deleteByPk(riskClassfyCfg); 
				if ( num > 0 ) {
					RiskClassfyList riskClassfyList = new RiskClassfyList();
					riskClassfyList.setRiskCfgNo(riskClassfyCfg.getRiskCfgNo());
					num += riskClassfyListDao.deleteByRiskCfgNo(riskClassfyList);
				}
			}
			
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRiskClassfyCfgVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(RiskClassfyCfgVO inRiskClassfyCfgVo) {
		logger.debug("当前修改数据为:"+ inRiskClassfyCfgVo.toString());
		int num = 0;
		try {
			RiskClassfyCfg riskClassfyCfg = new RiskClassfyCfg();
			beanCopy(inRiskClassfyCfgVo, riskClassfyCfg);
			num = riskClassfyCfgDao.updateByPk(riskClassfyCfg);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRiskClassfyCfgVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public RiskClassfyCfgVO queryByPk(RiskClassfyCfgVO inRiskClassfyCfgVo) {
		
		logger.debug("当前查询参数信息为:"+ inRiskClassfyCfgVo);
		try {
			RiskClassfyCfg queryRiskClassfyCfg = new RiskClassfyCfg();
			beanCopy(inRiskClassfyCfgVo, queryRiskClassfyCfg);
			RiskClassfyCfg queryRslRiskClassfyCfg = riskClassfyCfgDao.queryByPk(queryRiskClassfyCfg);
			if (Objects.nonNull(queryRslRiskClassfyCfg)) {
				RiskClassfyCfgVO outRiskClassfyCfgVo = beanCopy(queryRslRiskClassfyCfg, new RiskClassfyCfgVO());
				logger.debug("当前查询结果为:"+ outRiskClassfyCfgVo.toString());
				return outRiskClassfyCfgVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RiskClassfyCfgVO> queryAllOwner(RiskClassfyCfgVO riskClassfyCfgVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RiskClassfyCfgVO> list = null;
		try {
			List<RiskClassfyCfg> riskClassfyCfgs = riskClassfyCfgDao.queryAllOwnerByPage(riskClassfyCfgVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ riskClassfyCfgs.size());
			pageSet(riskClassfyCfgs, riskClassfyCfgVo);
			list = (List<RiskClassfyCfgVO>) beansCopy(riskClassfyCfgs, RiskClassfyCfgVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RiskClassfyCfgVO> queryAllCurrOrg(RiskClassfyCfgVO riskClassfyCfgVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<RiskClassfyCfg> riskClassfyCfgs = riskClassfyCfgDao.queryAllCurrOrgByPage(riskClassfyCfgVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+riskClassfyCfgs.size());
		List<RiskClassfyCfgVO> list = null;
		try {
			pageSet(riskClassfyCfgs, riskClassfyCfgVo);
			list = (List<RiskClassfyCfgVO>) beansCopy(riskClassfyCfgs, RiskClassfyCfgVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RiskClassfyCfgVO> queryAllCurrDownOrg(RiskClassfyCfgVO riskClassfyCfgVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<RiskClassfyCfg> riskClassfyCfgs = riskClassfyCfgDao.queryAllCurrDownOrgByPage(riskClassfyCfgVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ riskClassfyCfgs.size());
		List<RiskClassfyCfgVO> list = null;
		try {
			pageSet(riskClassfyCfgs, riskClassfyCfgVo);
			list = (List<RiskClassfyCfgVO>) beansCopy(riskClassfyCfgs, RiskClassfyCfgVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RiskClassfyListVO> queryListAll(RiskClassfyListVO riskClassfyListVo) {
		logger.debug("当前查询查询风险分类配置下的所有配置清单的参数信息为:" + riskClassfyListVo.getRiskCfgNo());
		List<RiskClassfyList> riskClassfyLists = riskClassfyListDao.queryListAllByPage(riskClassfyListVo);
		logger.debug("当前查询查询风险分类配置下的所有配置清单的结果集数量为:"+ riskClassfyLists.size());
		List<RiskClassfyListVO> list = null;
		try {
			pageSet(riskClassfyLists, riskClassfyListVo);
			list = (List<RiskClassfyListVO>) beansCopy(riskClassfyLists, RiskClassfyListVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}
	/**
	 * 校验插入或更新的风险分类配置清单
	 * @param inRiskClassfyListVo
	 * @return
	 */
	private int valiRiskClassfyList(RiskClassfyListVO inRiskClassfyListVo) {
		int num = 0;
		int count = riskClassfyListDao.queryDataIsHave(inRiskClassfyListVo);
		if ( count > 0) {
			num = -2;
		}else {
			List<RiskClassfyList> lists = riskClassfyListDao.queryBetweenLists(inRiskClassfyListVo);
			if ( null != lists && lists.size() > 0) {
				int min = inRiskClassfyListVo.getOverdueMinDay();
				int max = inRiskClassfyListVo.getOverdueMaxDay();
				for (int i = 0; i < lists.size(); i++ ) {
					if (!StringUtils.isEmpty(inRiskClassfyListVo.getRiskListNo())
						&& lists.get(i).getRiskListNo().equals(inRiskClassfyListVo.getRiskListNo())) {
						//如果存在风险分类配置编码,并且相等,则不判断
						continue;
					}
					int oldMin = lists.get(i).getOverdueMinDay();
					int oldMax = lists.get(i).getOverdueMaxDay();
					//如果当最小值处于oldMin 和oldMax之间,则是交叉
					//如果当最小值小于 oldMin 但是max 大于或等于oldMin, 则是交叉
					if ( min >= oldMin && min <= oldMax)
						return -3;
					else if( min < oldMin && max >= oldMin ) 
						return -3;
				}
			}
		}
		return num;
	}

	@Override
	public int insertRiskClassfyList(RiskClassfyListVO inRiskClassfyListVo) {
		logger.debug("当前新增数据为:"+ inRiskClassfyListVo.toString());
		int num = 0;
		try {
			num = valiRiskClassfyList(inRiskClassfyListVo);
			if ( num == 0) {
				RiskClassfyList riskClassfyList = new RiskClassfyList();
				beanCopy(inRiskClassfyListVo, riskClassfyList);
				String riskListNo = UUID.randomUUID().toString().replace("-", "");
				riskClassfyList.setRiskListNo(riskListNo);
				num = riskClassfyListDao.insertRiskClassfyList(riskClassfyList);
			}
			
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	@Override
	public int updateRiskClassfyListByPk(RiskClassfyListVO inRiskClassfyListVo) {
		logger.debug("当前修改数据为:"+ inRiskClassfyListVo.toString());
		int num = 0;
		try {
			num = valiRiskClassfyList(inRiskClassfyListVo);
			if ( num == 0) {
				RiskClassfyList riskClassfyList = new RiskClassfyList();
				beanCopy(inRiskClassfyListVo, riskClassfyList);
				num = riskClassfyListDao.updateByPk(riskClassfyList);
			}
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRiskClassfyListVo +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public int deleteRiskClassfyListByPk(RiskClassfyListVO inRiskClassfyListVo) {
		logger.debug("当前删除数据条件为:"+ inRiskClassfyListVo);
		int num = 0;
		try {
			RiskClassfyList riskClassfyList = new RiskClassfyList();
			beanCopy(inRiskClassfyListVo, riskClassfyList);
			num = riskClassfyListDao.deleteByPk(riskClassfyList);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRiskClassfyListVo +"删除的数据条数为"+ num);
		return num;
	}

	@Override
	public RiskClassfyCfgVO insertRiskInfoBack(RiskClassfyCfgVO inRiskClassfyCfgVo) {
		RiskClassfyCfgVO outputVo = new RiskClassfyCfgVO();
		int num = 0;
		RiskClassfyCfg riskClassfyCfg = new RiskClassfyCfg();
		try {
			num = valiRiskClassfyCfg();
			if ( num == 0 ) {
				beanCopy(inRiskClassfyCfgVo, riskClassfyCfg);
				String riskCfgNo = UUID.randomUUID().toString().replace("-", "");
				riskClassfyCfg.setRiskCfgNo(riskCfgNo);
				num = riskClassfyCfgDao.insertRiskClassfyCfg(riskClassfyCfg);
			}
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		if ( num > 0 ) {
			RiskClassfyCfg insertInfo = riskClassfyCfgDao.queryByPk(riskClassfyCfg);
			beanCopy(insertInfo, outputVo);
		}
		logger.debug("当前新增数据条数为:"+ num);
		outputVo.setTotal(num);
		return outputVo;
	}

	@Override
	public int updateSubmitByPk(RiskClassfyCfgVO inRiskClassfyCfgVo) {
		logger.debug("当前修改数据为:"+ inRiskClassfyCfgVo.toString());
		int num = 0;
		try {
			num = valiRiskClassfyCfg();
			if ( num == 0 ) {
				num = riskClassfyCfgDao.queryRiskClassfyListCount(inRiskClassfyCfgVo);
				if ( num > 0) {
					RiskClassfyCfg riskClassfyCfg = new RiskClassfyCfg();
					beanCopy(inRiskClassfyCfgVo, riskClassfyCfg);
					num = riskClassfyCfgDao.updateSubmitByPk(riskClassfyCfg);
				}else if ( num == 0) //风险分类配置下不存在风险分类配置清单信息,不能生效!
					num = -3;
			}
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRiskClassfyCfgVo +"修改的数据条数为"+ num);
		return num;
	}

	@Override
	public int updateDisabledByPk(RiskClassfyCfgVO inRiskClassfyCfgVo) {
		logger.debug("当前修改数据为:"+ inRiskClassfyCfgVo.toString());
		int num = 0;
		try {
			RiskClassfyCfg riskClassfyCfg = new RiskClassfyCfg();
			beanCopy(inRiskClassfyCfgVo, riskClassfyCfg);
			num = riskClassfyCfgDao.updateDisabledByPk(riskClassfyCfg);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRiskClassfyCfgVo +"修改的数据条数为"+ num);
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RiskClassfyListVO> queryListAllByCfgNo(RiskClassfyListVO riskClassfyListVo) {
		logger.debug("当前查询查询风险分类配置下的所有配置清单的参数信息为:" + riskClassfyListVo.getRiskCfgNo());
		List<RiskClassfyList> riskClassfyLists = riskClassfyListDao.queryListAllByCfgNo(riskClassfyListVo);
		logger.debug("当前查询查询风险分类配置下的所有配置清单的结果集数量为:"+ riskClassfyLists.size());
		List<RiskClassfyListVO> list = null;
		try {
			pageSet(riskClassfyLists, riskClassfyListVo);
			list = (List<RiskClassfyListVO>) beansCopy(riskClassfyLists, RiskClassfyListVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public RiskClassfyListVO queryListsByCondition(Integer overdueDay, String guarWay, String wheAgr,
                                                   String custLevel) {
		List<RiskClassfyListVO> lists = null;
		RiskClassfyListVO outputVO = null;
		try {
			List<RiskClassfyList> riskClassfyLists = riskClassfyListDao.queryListsByCondition(overdueDay, guarWay, wheAgr, custLevel);
			if ( riskClassfyLists != null && riskClassfyLists.size() > 0 ) {
				lists = (List<RiskClassfyListVO>) beansCopy(riskClassfyLists, RiskClassfyListVO.class);
				outputVO = lists.get(0);
			}
		} catch (Exception e) {
			//e.printStackTrace();
			logger.error("queryListsByCondition,查询数据异常", e.getMessage());
		}
		
		return outputVO;
	}
	@Override
	public List<RiskClassfyListVO> queryRiskClassfyListByCondition(Integer overdueDay, String guarWay, String wheAgr, String custLevel) {
		List<RiskClassfyListVO> lists = null;
		try {
			List<RiskClassfyList> riskClassfyLists = riskClassfyListDao.queryListsByCondition(overdueDay, guarWay, wheAgr, custLevel);
			if ( riskClassfyLists != null && riskClassfyLists.size() > 0 ) {
				lists = (List<RiskClassfyListVO>) beansCopy(riskClassfyLists, RiskClassfyListVO.class);
			}
		} catch (Exception e) {
			logger.error("queryRiskClassfyList查询数据异常", e.getMessage());
		}
		return lists;
	}
}
